package cn.lhx.blog.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.lhx.blog.pojo.Article;
import cn.lhx.blog.pojo.ArticleCategory;
import cn.lhx.blog.pojo.Comment;
import cn.lhx.blog.pojo.Link;
import cn.lhx.blog.service.ArtiCatgService;
import cn.lhx.blog.service.ArticleService;
import cn.lhx.blog.service.CommentService;
import cn.lhx.blog.service.LinksService;
import cn.lhx.blog.util.StringTookit;

public class RefreshSideBar extends HttpServlet {

	private ArtiCatgService artiCatgService;
	private LinksService linksService;
	private CommentService commentService;
	private ArticleService articleService;

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(this.getServletContext());
		// 设置输出信息的格式及字符集
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// 创建输出流对象
		PrintWriter out = response.getWriter();
		// 得到用户己选中的区域代码
		String areaID = request.getParameter("areaID");
		int areaIndex = Integer.parseInt(areaID);

		switch (areaIndex) {
		case 1:
			System.out.println("最近的日志");
			ArticleService articleService=(ArticleService)context.getBean("articleService");
			List<Article> articles = articleService.getArticles(0,5);
			out.println("<article>");
			for (Article article : articles) {
				out.println("<title>"+article.getTitle()+"</title>");
				out.println("<shortTitle>"+StringTookit.subString(article.getTitle(), 30)+"</shortTitle>");
				out.println("<url>"+request.getContextPath()+"/blog/"+article.getId()+"</url>");
			}
			out.println("</article>");
			break;
		case 2:
			System.out.println("最近的评论");
			 commentService=(CommentService)context.getBean("commentService");
				List<Comment> comments=commentService.getComments(0, 8);
				out.println("<commentList>");
				for (Comment comment : comments) {
					out.println("<guest>"+comment.getAuthor()+"</guest>");
					out.println("<shortTitle>" +StringTookit.subString(comment.getArticle().getTitle(), 30)+"</shortTitle>");
					out.println("<title>"+comment.getArticle().getTitle()+"</title>");
					out.println("<comtURL>"+request.getContextPath()+"/blog/"+comment.getArticle().getId()+"#comment-"+comment.getId()+"</comtURL>");
				}
			out.println("</commentList>");
			break;
		case 3:
			System.out.println("分类");
			artiCatgService = (ArtiCatgService) context
					.getBean("artiCatgService");
			List<ArticleCategory> categories = artiCatgService.getAllCategory();
			out.println("<categoryList>");
			for (ArticleCategory articleCategory : categories) {
				out.println("<name>" + articleCategory.getName()+ "</name>");
				out.println("<contain>"+ articleCategory.getContains() + "</contain>");
				out.println("<decription>" + articleCategory.getDescription()
						+ "</decription>");
				// ${pageContext.request.contextPath}/category/${catg.id }"
				out.println("<url>" + request.getContextPath() + "/category/"
						+ articleCategory.getId() + "</url>");
			}
			out.println("</categoryList>");
			break;
		case 4:
			System.out.println("链接");
			linksService = (LinksService) context.getBean("linksService");
			List<Link> links = linksService.getLinks(0, 5);
			out.println("<linkList>");
			for (Link link : links) {
				out.println("<name>" + link.getName() + "</name>");
				out.println("<decription>" + link.getDescription()
						+ "</decription>");
				out.println("<url>" + link.getUrl() + "</url>");
			}
			out.println("</linkList>");
			break;
		case 5:
			System.out.println("最近的日志");
			break;
		default:
			break;
		}

		out.flush();
		out.close();
	}
}
